package com.swm.util;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.swm.mapper.StockMapper;
import com.swm.pojo.Stock;
import org.apache.ibatis.session.SqlSession;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 师文明
 * @data 2024/12/23 下午3:11
 **/
public class getStockDayData {
    public static int allup;
    public static double allupnum;
    //创建一个固定大小的线程池
    static ExecutorService executorService = Executors.newFixedThreadPool(50);
    static SqlSession sqlSession = SqlSessionUtil.openSession();
    static StockMapper mapper = sqlSession.getMapper(StockMapper.class);
    static int num = 0;

    public static List<Stock> getStcokList() {
        List<Stock> stocks = mapper.selectAllStock();
        return stocks;
    }

    public static void main(String[] args) throws Exception {
        //获取所有股票列表
        List<Stock> stcokList = getStcokList();

        for (Stock stock : stcokList) {
            executorService.execute(() -> {
                try {
                    getData(stock.getCode(), stock.getType());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
        }
        Thread.sleep(30000);
        double v = allupnum / allup;
        System.out.println("平均涨幅：" + v);
        //getData(stcokList.get(0).getCode(), stcokList.get(0).getType());
        // 关闭线程池，以优雅的方式
        executorService.shutdown();
    }

    public static void getData(String code, int type) throws Exception {
        // 连接到新浪财经接口
        //String url = "http://push2his.eastmoney.com/api/qt/stock/trends2/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f52,f53,f54,f55,f56,f57,f58&ut=fa5fd1943c7b386f172d6893dbfba10b&secid=0.002384";
        //String url = "https://push2his.eastmoney.com/api/qt/stock/kline/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&beg=0&end=20500101&rtntype=6&secid=0.002384&klt=101&fqt=1";
        String url = "https://push2his.eastmoney.com/api/qt/stock/kline/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f116,f117&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&beg=0&end=20500101&rtntype=6&secid=" + type + "." + code + "&klt=101&fqt=1";
        //String url = "http://push2his.eastmoney.com/api/qt/stock/trends2/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f52,f53,f54,f55,f56,f57,f58&ut=fa5fd1943c7b386f172d6893dbfba10b&secid="+type+"."+code;
        //String url = "http://push2his.eastmoney.com/api/qt/stock/trends2/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f52,f53,f54,f55,f56,f57,f58&ut=fa5fd1943c7b386f172d6893dbfba10b&secid=1.605179";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Cookie", "qgqp_b_id=488de58836c5f30c189b9ad4c700b9f5; st_si=04137509447995; websitepoptg_api_time=1734056942833; st_asi=delete; HAList=ty-1-603999-%u8BFB%u8005%u4F20%u5A92%2Cty-0-002888-%u60E0%u5A01%u79D1%u6280%2Cty-0-002269-%u7F8E%u90A6%u670D%u9970%2Cty-0-000813-%u5FB7%u5C55%u5065%u5EB7%2Cty-0-002045-%u56FD%u5149%u7535%u5668%2Cty-0-300059-%u4E1C%u65B9%u8D22%u5BCC%2Cty-1-688605-N%u5148%u950B%2Cty-1-000001-%u4E0A%u8BC1%u6307%u6570; st_pvi=62081555482493; st_sp=2024-12-11%2010%3A26%3A42; st_inirUrl=https%3A%2F%2Fwww.baidu.com%2Flink; st_sn=60; st_psi=20241213164756225-113200301201-5528754653");
        // 从数据源获取实时股票数据
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        String data = response.toString();
        JSONObject jsonObject = JSONObject.parseObject(data);
        if (jsonObject.get("data") != null) {
            JSONObject jsonObject1 = JSONObject.parseObject(jsonObject.get("data").toString());
            String name = jsonObject1.get("name").toString();
            JSONArray klines = JSONArray.parse(jsonObject1.get("klines").toString());

            JSONArray objects = new JSONArray();
         /*   for (int i=1;i<20;i++){
                String[] split = klines.get(klines.size() - i).toString().split(",");
                System.out.println("时间："+split[0]);
                System.out.println("最高："+split[7]);
                System.out.println("最低："+split[9]);
            }*/
            double price0 = Double.parseDouble(klines.get(klines.size() - 1).toString().split(",")[2]);
            //5日线 10日线 20日线 30日线

            https:
//51.push2.eastmoney.com/api/qt/stock/sse?fields=f116f117&secid=1.688395
         /*   if(klines.size()>60){
                double totalth1=0.0;
                double totaltw1=0.0;
                double totalte1=0.0;
                double totalf1=0.0;
                for(int i=1;i<31;i++){
                    double price = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[2]);
                    if(i<6){
                        totalf1+=price;
                    }
                    if (i<11){
                        totalte1+=price;
                    }
                    if(i<21){
                        totaltw1+=price;

                    }
                    totalth1+=price;

                }

                double th1 = totalth1 / 30;
                double tw1 = totaltw1 / 20;
                double te1 = totalte1 / 10;
                double f1 = totalf1 / 5;

                //int j=10;
                int m=0;
                double max=0;
                double min =1000;

                for(int j=2;j<30;j++){
                    double totalth2=0.0;
                    double totaltw2=0.0;
                    double totalte2=0.0;
                    double totalf2=0.0;
                    for(int i=(1+j);i<(31+j);i++){
                        String[] split = klines.get(klines.size() - i).toString().split(",");
                        double price = Double.parseDouble(split[2]);
                        max= Math.max(price, max);
                        min= Math.min(price, min);

                        if(i<(6+j)){
                            totalf2+=price;
                        }
                        if (i<(11+j)){
                            totalte2+=price;
                        }
                        if(i<(21+j)){
                            totaltw2+=price;

                        }
                        totalth2+=price;

                    }



                    double th2 = totalth2 / 30;
                    double tw2 = totaltw2 / 20;
                    double te2 = totalte2 / 10;
                    double f2 = totalf2 / 5;
                    if(f2<te2&&te2<tw2&&f2<tw2&&tw2<th2){
                        m++;
                    }
                }

                //10日振幅
                double priceDiff  = (max - min) / price0;


                //System.out.println(11);

                //三日前 5<10<20<30
                //三日后 5>10>20>30
                if(*//*f2<te2&&te2<tw2&&f2<tw2&&tw2<th2 m>0 &&*//* priceDiff<0.1 && price0>f1&& f1>te1 && te1<tw1 *//*&& tw1<th1 *//*){
                    if(!name.startsWith("ST")&&!name.startsWith("*ST")) {
                        //System.out.println(name+": 30日内最大振幅："+String.format("%.2f", priceDiff*100)+"%");
                        System.out.println(name);
                    }
                }

            }*/

            if (klines.size() > 30) {

                int num = 3;
                double max = 0;
                double min = 1000000000;
                double average = 0;
                double total = 0;

                double maxprice = 0;
                double minprice = 10000;

                for (int i = 2; i < 10; i++) {
                    String[] split = klines.get(klines.size() - i).toString().split(",");
                    double price = Double.parseDouble(split[2]);
                    maxprice = Math.max(price, maxprice);
                    minprice = Math.min(price, minprice);
                }
                //10天最大振幅
                double priceDiff = (maxprice - minprice) / price0;

                for (int i = (num + 1); i < 11; i++) {
                    String[] split = klines.get(klines.size() - i).toString().split(",");
                    double money = Double.parseDouble(split[5]);
                    max = Math.max(money, max);
                    min = Math.min(money, min);
                    total += money;
                }
                average = total / 9;
                //10天内成交量振幅
                double v3 = (max - min) / average;

                //昨日成交额
                double v = Double.parseDouble(klines.get(klines.size() - num).toString().split(",")[5]);
                //前天成交额
                double v1 = Double.parseDouble(klines.get(klines.size() - (num + 1)).toString().split(",")[5]);
                //昨日涨幅
                double v2 = Double.parseDouble(klines.get(klines.size() - num).toString().split(",")[8]);
                //今日涨幅
                double v4 = Double.parseDouble(klines.get(klines.size() - num + 1).toString().split(",")[8]);

                //放量3倍以上 涨幅大于3小于5  一个月内振幅小于10
                if (/*v/v1>2 &&*/ v2 > 3 && v2 < 5 /*&&v3<1*/ /*&&priceDiff<0.1*/) {
                    String url1 = "https://51.push2.eastmoney.com/api/qt/stock/sse?fields=f50,f116,f117,f168&secid=" + type + "." + code;

                    JSONObject webData = getWebData(url1);
                    //市值
                    Double v5 = Double.parseDouble(webData.get("f117").toString()) / 100000000;
                    //量比
                    Double v6 = Double.parseDouble(webData.get("f50").toString()) / 100;
                    //换手率
                    Double v7 = Double.parseDouble(webData.get("f168").toString()) / 100;

                    //市值大于50亿 小于200亿   量比大于1  换手率大于5小于10
                    if (!name.startsWith("ST") && !name.startsWith("*ST") && v5 > 50 && v5 < 200 && v6 > 1 && v7 > 5 && v7 < 10) {
                        System.out.println(name + " 第二天涨跌幅：" + v4);
                        allupnum += v4;
                        allup++;
                    }

                }
            }

            /*if(klines.size()>125){
                //半年内有过涨停
                for(int i=1;i<15;i++){
                    double v = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[8]);
                    if(v>9.8){
                        double v1 = Double.parseDouble(klines.get(klines.size() - 1).toString().split(",")[8]);
                        double v2 = Double.parseDouble(klines.get(klines.size() - 2).toString().split(",")[8]);
                        double v3 = Double.parseDouble(klines.get(klines.size() - 3).toString().split(",")[8]);
                        double v4 = Double.parseDouble(klines.get(klines.size() - 4).toString().split(",")[8]);
                        double v5 = Double.parseDouble(klines.get(klines.size() - 5).toString().split(",")[8]);
                        double price = Double.parseDouble(klines.get(klines.size() - 1).toString().split(",")[2]);

                        if(price<20){*/
            //两跌三涨
                       /* if(v1>0&&v1<3&&v2>0&&v2<3&&v3>0&&v3<3&&v4<0&&v5<0){
                            if(!name.startsWith("ST")&&!name.startsWith("*ST")){
                                System.out.println(name+":"+klines.get(klines.size() - i).toString().split(",")[0]);
                            }

                        }*/
            //两跌两涨
                       /* if(v1>0&&v1<3&&v2>0&&v2<3&&v3<0&&v4<0){
                            if(!name.startsWith("ST")&&!name.startsWith("*ST")){
                                //System.out.println(name);
                                 System.out.println(name+":"+klines.get(klines.size() - i).toString().split(",")[0]);
                            }
                        }*/
            //一跌两涨
                           /* if(v1>0&&v1<3&&v2>0&&v2<3&&v3<0){
                                if(!name.startsWith("ST")&&!name.startsWith("*ST")){
                                    //System.out.println(name);
                                    System.out.println(name+":"+klines.get(klines.size() - i).toString().split(",")[0]);
                                }
                            }*/
                           /* //一跌三涨
                            if(v1>0&&v1<3&&v2>0&&v2<3&&v3>0&&v3<3&&v4<0){
                                if(!name.startsWith("ST")&&!name.startsWith("*ST")){
                                    //System.out.println(name);
                                    System.out.println(name+":"+klines.get(klines.size() - i).toString().split(",")[0]);
                                }
                            }*/
            //大大前天跌大前天十字星
                            /*String d3 = klines.get(klines.size() - 4).toString();
                            //十字星
                            //开盘价减去收盘价幅度小于1个点
                            //开盘价
                            double v6 = Double.parseDouble(d3.split(",")[1]);
                            //开盘价
                            double v7 = Double.parseDouble(d3.split(",")[2]);
                            double abs = Math.abs(v7 - v6);
                            double v8 = abs / v6;
                            if(v5<0&&v4<1&&v4>-1&&v8<0.001){
                                System.out.println(name+"前天日涨跌"+v3);
                            }*/


            //大前天跌前天十字星
                           /* String d3 = klines.get(klines.size() - 3).toString();
                            //十字星
                            //开盘价减去收盘价幅度小于1个点
                            //开盘价
                            double v6 = Double.parseDouble(d3.split(",")[1]);
                            //开盘价
                            double v7 = Double.parseDouble(d3.split(",")[2]);
                            double abs = Math.abs(v7 - v6);
                            double v8 = abs / v6;
                            if(v4<0&&v3<1&&v3>-1&&v8<0.001){
                                System.out.println(name+"昨天日涨跌"+v2);
                            }*/

                            /*//前天跌昨天十字星
                            String d3 = klines.get(klines.size() - 2).toString();
                            //十字星
                            //开盘价减去收盘价幅度小于1个点
                            //开盘价
                            double v6 = Double.parseDouble(d3.split(",")[1]);
                            //开盘价
                            double v7 = Double.parseDouble(d3.split(",")[2]);
                            double abs = Math.abs(v7 - v6);
                            double v8 = abs / v6;
                            if(v3<0&&v2<1&&v2>-1&&v8<0.001){
                                System.out.println(name+"今日涨跌"+v1);
                            }*/

            //昨天跌今天十字星
                 /*           String d2 = klines.get(klines.size() - 1).toString();
                            //十字星
                            //开盘价减去收盘价幅度小于1个点
                            //收盘涨跌1个点以内
                            //开盘价
                            double v6 = Double.parseDouble(d2.split(",")[1]);
                            //收盘价
                            double v7 = Double.parseDouble(d2.split(",")[2]);
                            //最高价
                            double v8 = Double.parseDouble(d2.split(",")[3]);
                            //最低价
                            double v9 = Double.parseDouble(d2.split(",")[4]);
                            double abs = Math.abs(v7 - v6);
                            double v10 = abs / v6;

                            double max = Math.max(v6, v7);
                            double min = Math.min(v6, v7);
                            //上影线
                            double v11 = v8 - max;
                            //下影线
                            double v12 = min - v9;



                            if(v2<0&&v1<1&&v1>-1&&v10<0.001){
                                System.out.println(name);
                            }

                        }

                        break;
                    }
                }



            }*/
            /*for (int i=1;i<20;i++){
                String[] split = klines.get(klines.size() - i).toString().split(",");
                System.out.println("时间："+split[0]);
                System.out.println("最高："+split[7]);
                System.out.println("最低："+split[9]);
            }*/
            /*if(klines.size()>100){
                for (int i =0;i<klines.size();i++){
                    if(i>klines.size()-100){
                        objects.add(klines.get(i));
                    }
                }
            }*/
           /* if(klines.size()>100){
                //24-12-31号
                String[] split1 = klines.get(klines.size() - 1).toString().split(",");
                //24-9-18号
                String[] split2 = klines.get(klines.size() - 70).toString().split(",");
                if(Double.parseDouble(split1[2])<Double.parseDouble(split2[2])&&!name.startsWith("ST")&&!name.startsWith("*ST")&&Double.parseDouble(split1[2])<40){
                    System.out.println("股票："+name);
                    System.out.println("9.18号："+split2[2]);
                    System.out.println("今天："+split1[2]);
                }
            }
            */

            /*if(klines.size()>5){
                //24-12-17号
                String[] split1 = klines.get(klines.size() - 5).toString().split(",");
                //24-12-18号
                String[] split2 = klines.get(klines.size() - 4).toString().split(",");
                if(Double.parseDouble(split1[8])<-9.8&&Double.parseDouble(split2[8])>9.8){
                    System.out.println("股票："+name);
                    System.out.println("上周二跌："+split1[8]);
                    System.out.println("上周三涨："+split2[8]);
                }
            }*/

           /* if(objects.size()>0){
                double max=0;
                double min=1000;
                String maxdate = "";
                String mindate = "";
                int index=0;
                for (Object k:objects){
                    if(index>0){
                        String[] split = k.toString().split(",");
                        max= Math.max(max, Double.parseDouble(split[2]));
                        min= Math.min(min, Double.parseDouble(split[2]));
                        maxdate=max>Double.parseDouble(split[2])?maxdate:split[0];
                        mindate=min<Double.parseDouble(split[2])?mindate:split[0];
                    }
                    index++;
                }
                String[] split = objects.get(objects.size() - 1).toString().split(",");
                double v = Double.parseDouble(split[2]);
                if(v<max/2&&!name.startsWith("ST")&&!name.startsWith("*ST")){
                    System.out.println(name+"   最高价："+maxdate+" : "+max+"   最低价："+mindate+" : "+min+"   当前价："+v);
                    *//*System.out.println("最高价："+maxdate+" : "+max);
                    System.out.println("最低价："+mindate+" : "+min);
                    System.out.println("当前价："+v);*//*
                }
            }
            //涨停计数
            if(klines.size()>20){
                //int i = tennum(klines);
                int i = not10(0,klines);
                if(i==2){
                    num++;
                    System.out.println(name+":"+i+"连板");
                    //System.out.println(name+":首板");
                }
            }*/

            //找出最近的顶点，和最近的底点
            /*if(klines.size()>20){
                System.out.println(name);
                for (int i=10;i<klines.size()-10;i++){
                    boolean compare = compare(klines, i);
                    if (compare){
                        break;
                    }
                }
            }*/

            //最近几个月没有大波动
            /*int days=100;
            if(klines.size()>days){
                double max=0;
                double min=1000;
                String maxdate = "";
                String mindate = "";

                for (int i=0;i<days;i++){
                    String[] split = klines.get(klines.size() -1- i).toString().split(",");
                    max= Math.max(max, Double.parseDouble(split[2]));
                    min= Math.min(min, Double.parseDouble(split[2]));
                    maxdate=max>Double.parseDouble(split[2])?maxdate:split[0];
                    mindate=min<Double.parseDouble(split[2])?mindate:split[0];
                }
                double v = (max - min) / (max + min) * 2;
                //当前股价
                String[] split = klines.get(klines.size() -1).toString().split(",");
                double curPrice = Double.parseDouble(split[2]);
                if(v<0.15){
                    System.out.println(name);
                    System.out.println(max);
                    System.out.println(min);
                    System.out.println(maxdate);
                    System.out.println(mindate);
                }
            }*/
        }
    }

    public static int not10(int i, JSONArray klines) {
        if (Double.parseDouble(klines.get(klines.size() - 1 - i).toString().split(",")[8]) > 9.8 && Double.parseDouble(klines.get(klines.size() - 1 - i).toString().split(",")[8]) < 11) {
            return not10(i + 1, klines);
        } else {
            return i;
        }
    }

    public static int tennum(JSONArray klines) {
        int num = 0;
        for (int i = 1; i < 10; i++) {
            if (Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[8]) > 9.8) {
                num++;
            }
        }
        return num;
    }

    //判断两个值大小
    public static boolean compare(JSONArray klines, int i) {
        for (int j = 0; j < 5; j++) {
            System.out.println(klines.get(klines.size() - i - j).toString().split(",")[2]);
            System.out.println(klines.get(klines.size() - i).toString().split(",")[2]);
            System.out.println(klines.get(klines.size() - i + j).toString().split(",")[2]);
            if (Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[2]) < Double.parseDouble(klines.get(klines.size() - i - j).toString().split(",")[2]) || Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[2]) < Double.parseDouble(klines.get(klines.size() - i - j).toString().split(",")[2])) {
                return false;
            }
        }
        return true;
    }


    public static JSONObject getWebData(String url) {
        URL obj = null;
        JSONObject jsonObject1;
        try {
            obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Cookie", "qgqp_b_id=0c801f7a5b2dbbe1a7578a4f50bf5c81; websitepoptg_api_time=1742518229630; st_si=94336787243959; fullscreengg=1; fullscreengg2=1; st_asi=delete; HAList=ty-1-688395-%u6B63%u5F26%u7535%u6C14%2Cty-0-920128-%u80DC%u4E1A%u7535%u6C14; st_pvi=35314926305263; st_sp=2025-03-21%2008%3A50%3A29; st_inirUrl=https%3A%2F%2Fwww.baidu.com%2Flink; st_sn=12; st_psi=20250321164157298-111000300841-7480637044");
            // 从数据源获取实时股票数据
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
                break;
            }
            in.close();
            String data = response.toString();
            String[] split = data.split("data:");
            JSONObject jsonObject = JSONObject.parseObject(split[1]);
            jsonObject1 = JSONObject.parseObject(jsonObject.get("data").toString());
            //s = split[1].split("}}")[0];

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return jsonObject1;


    }
}

